package com.zenithmind.exam.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zenithmind.exam.pojo.entity.AnswerRecord;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Delete;

import java.util.List;

/**
 * 答题记录Mapper接口
 * 
 * @author ZenithMind
 * @since 2024-07-01
 */
@Mapper
public interface AnswerRecordMapper extends BaseMapper<AnswerRecord> {

    /**
     * 根据考试记录ID查询答题记录列表
     * 
     * @param examRecordId 考试记录ID
     * @return 答题记录列表
     */
    @Select("SELECT * FROM exam_answer_record WHERE exam_record_id = #{examRecordId} ORDER BY create_time ASC")
    List<AnswerRecord> selectByExamRecordId(@Param("examRecordId") String examRecordId);

    /**
     * 根据题目ID查询答题记录列表
     * 
     * @param questionId 题目ID
     * @return 答题记录列表
     */
    @Select("SELECT * FROM exam_answer_record WHERE question_id = #{questionId} ORDER BY create_time DESC")
    List<AnswerRecord> selectByQuestionId(@Param("questionId") String questionId);

    /**
     * 根据考试记录ID和题目ID查询答题记录
     * 
     * @param examRecordId 考试记录ID
     * @param questionId 题目ID
     * @return 答题记录
     */
    @Select("SELECT * FROM exam_answer_record WHERE exam_record_id = #{examRecordId} AND question_id = #{questionId}")
    AnswerRecord selectByExamRecordIdAndQuestionId(@Param("examRecordId") String examRecordId, @Param("questionId") String questionId);

    /**
     * 根据考试记录ID删除所有答题记录
     * 
     * @param examRecordId 考试记录ID
     * @return 删除行数
     */
    @Delete("DELETE FROM exam_answer_record WHERE exam_record_id = #{examRecordId}")
    int deleteByExamRecordId(@Param("examRecordId") String examRecordId);

    /**
     * 统计考试记录的正确题目数
     * 
     * @param examRecordId 考试记录ID
     * @return 正确题目数
     */
    @Select("SELECT COUNT(*) FROM exam_answer_record WHERE exam_record_id = #{examRecordId} AND is_correct = 1")
    Integer countCorrectByExamRecordId(@Param("examRecordId") String examRecordId);

    /**
     * 统计考试记录的错误题目数
     * 
     * @param examRecordId 考试记录ID
     * @return 错误题目数
     */
    @Select("SELECT COUNT(*) FROM exam_answer_record WHERE exam_record_id = #{examRecordId} AND is_correct = 0")
    Integer countWrongByExamRecordId(@Param("examRecordId") String examRecordId);

    /**
     * 统计考试记录的未答题目数
     * 
     * @param examRecordId 考试记录ID
     * @return 未答题目数
     */
    @Select("SELECT COUNT(*) FROM exam_answer_record WHERE exam_record_id = #{examRecordId} AND user_answer IS NULL")
    Integer countUnansweredByExamRecordId(@Param("examRecordId") String examRecordId);

    /**
     * 计算考试记录的总得分
     * 
     * @param examRecordId 考试记录ID
     * @return 总得分
     */
    @Select("SELECT COALESCE(SUM(user_score), 0) FROM exam_answer_record WHERE exam_record_id = #{examRecordId}")
    Double sumScoreByExamRecordId(@Param("examRecordId") String examRecordId);

    /**
     * 查询题目的答题统计
     * 
     * @param questionId 题目ID
     * @return 答题统计
     */
    AnswerRecord selectQuestionStatistics(@Param("questionId") String questionId);

    /**
     * 查询错误答题记录
     * 
     * @param examRecordId 考试记录ID
     * @return 错误答题记录列表
     */
    @Select("SELECT * FROM exam_answer_record WHERE exam_record_id = #{examRecordId} AND is_correct = 0 ORDER BY create_time ASC")
    List<AnswerRecord> selectWrongAnswers(@Param("examRecordId") String examRecordId);

    /**
     * 批量插入答题记录
     * 
     * @param answerRecords 答题记录列表
     * @return 插入行数
     */
    int batchInsert(@Param("list") List<AnswerRecord> answerRecords);

    /**
     * 批量更新答题记录
     * 
     * @param answerRecords 答题记录列表
     * @return 更新行数
     */
    int batchUpdate(@Param("list") List<AnswerRecord> answerRecords);
}
